package com.cqan.tuan.controller;

import static com.cqan.common.ViewName.*;

import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.orm.PropertyFilter;
import com.cqan.tuan.entity.Shop;
import com.cqan.tuan.service.ShopService;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/shop")
public class ShopController extends BaseController<Shop> {

	@Resource
	private ShopService shopService;
	/**
	 * 商家管理列表
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.htm")
	public ModelAndView list(Integer pageNo,HttpServletRequest request)throws AppException{
		ModelAndView mav = new ModelAndView(TUAN_SHOP_LIST);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page.setPageNo(pageNo);
		page.setOrder("desc");
		page.setOrderBy("ctime");
		page = shopService.search(page, filters);
		mav.addObject("page", page);
		return mav;
	}
	
	
	@RequestMapping(value="/edit.htm")
	public ModelAndView edit(Long id,int pageNo)throws AppException{
		ModelAndView mav = new ModelAndView(TUAN_SHOP_EDIT);
		if (id!=null&&id>0) {
			Shop shop = shopService.get(id);
			mav.addObject("shop", shop);
		}
		mav.addObject("pageNo",pageNo);
		return mav;
	}
	@RequestMapping(value="/save.htm",method=RequestMethod.POST)
	public ModelAndView save(HttpServletRequest request,Long id)throws AppException{
		ModelAndView mav = new ModelAndView(new RedirectView("list.htm?pageNo="+request.getParameter("pageNo")));
		try {
			Shop shop = getRequestEntity(request);
			if (id!=null) {
				Shop a = shopService.get(id);
				copyProperties(a, shop);
				shopService.update(a);
				return mav;
			}
			shop.setCtime(Calendar.getInstance().getTime());
			shopService.add(shop);
		} catch (Exception e) {
			e.printStackTrace();
			throw new AppException();
		}
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete.htm",method=RequestMethod.POST)
	public String delete(String ids)throws AppException{
		try {
			if (ids!=null) {
				StringTokenizer st = new StringTokenizer(ids,"-");
				List<Long> list = Lists.newArrayList();
				while (st.hasMoreTokens()) {
					String id = st.nextToken();
					if (id!=null&&!"".equals(id)) {
						list.add(Long.parseLong(id));
					}
				}
				shopService.delete(list);
				return "删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "服务器异常，请稍后再试";
		}
		return  "删除失败！";
	}
	
	/**
	 * 验证商家名是否重复
	 * @param name
	 * @param id
	 * @return
	 * @throws AppException
	 */
	@ResponseBody
	@RequestMapping(value="/checkShopName.htm")
	public boolean  checkPinyin(String name,Long id)throws AppException{	
		Shop p = this.shopService.findBy("name", name);
		if(id==null){
			if (p!=null) {
				return false;
			}
		}else{
			if (p!=null&&p.getId()==id) {
				return false;
			}
		}
		return true;
	}
	
}
